import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JPasswordField;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.SpringLayout;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.ImageIcon;
import javax.swing.SwingConstants;

import java.awt.GridBagLayout;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;

import java.awt.Font;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;

import java.awt.event.*;
import java.awt.*;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;

public class GUIWelcomeDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField folderTextField;
	private JPasswordField passwordTextField;

	/**
	 * Create the dialog.
	 */
	public GUIWelcomeDialog() {
		setResizable(false);
		setAlwaysOnTop(true);
		setBounds(100, 100, 450, 342);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		SpringLayout sl_contentPanel = new SpringLayout();
		contentPanel.setLayout(sl_contentPanel);

		JLabel lblPleaseCreateA = new JLabel(
				"Please create a new password for data encryption");
		sl_contentPanel.putConstraint(SpringLayout.WEST, lblPleaseCreateA, 29,
				SpringLayout.WEST, contentPanel);
		sl_contentPanel.putConstraint(SpringLayout.SOUTH, lblPleaseCreateA,
				-123, SpringLayout.SOUTH, contentPanel);
		lblPleaseCreateA.setForeground(new Color(105, 105, 105));
		contentPanel.add(lblPleaseCreateA);

		JLabel lblPleaseSelectYour = new JLabel(
				"Please select your patients container directory");
		sl_contentPanel.putConstraint(SpringLayout.WEST, lblPleaseSelectYour,
				0, SpringLayout.WEST, lblPleaseCreateA);
		sl_contentPanel.putConstraint(SpringLayout.SOUTH, lblPleaseSelectYour,
				-67, SpringLayout.SOUTH, contentPanel);
		lblPleaseSelectYour.setForeground(new Color(105, 105, 105));
		contentPanel.add(lblPleaseSelectYour);

		folderTextField = new JTextField();
		sl_contentPanel.putConstraint(SpringLayout.NORTH, folderTextField, 6,
				SpringLayout.SOUTH, lblPleaseSelectYour);
		sl_contentPanel.putConstraint(SpringLayout.WEST, folderTextField, 29, SpringLayout.WEST, contentPanel);
		sl_contentPanel.putConstraint(SpringLayout.EAST, folderTextField, -163, SpringLayout.EAST, contentPanel);
		folderTextField.setEditable(false);
		folderTextField.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				// ============================================================
				// TEXT FIELD
				// ============================================================
				triggerFilePicker();

			}
		});
		contentPanel.add(folderTextField);
		folderTextField.setColumns(10);

		JButton btnNewButton = new JButton("Choose folder");
		sl_contentPanel.putConstraint(SpringLayout.NORTH, btnNewButton, 1,
				SpringLayout.NORTH, folderTextField);
		sl_contentPanel.putConstraint(SpringLayout.WEST, btnNewButton, 6, SpringLayout.EAST, folderTextField);
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				
				// ============================================================
				// CHOOSE FOLDER
				// ============================================================
				triggerFilePicker();
			}
		});
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		contentPanel.add(btnNewButton);

		JPanel panel = new JPanel();
		sl_contentPanel.putConstraint(SpringLayout.NORTH, panel, 10,
				SpringLayout.NORTH, contentPanel);
		sl_contentPanel.putConstraint(SpringLayout.WEST, panel, 10,
				SpringLayout.WEST, contentPanel);
		sl_contentPanel.putConstraint(SpringLayout.SOUTH, panel, -169,
				SpringLayout.SOUTH, contentPanel);
		sl_contentPanel.putConstraint(SpringLayout.EAST, panel, -10,
				SpringLayout.EAST, contentPanel);
		panel.setBorder(null);
		contentPanel.add(panel);
		SpringLayout sl_panel = new SpringLayout();
		panel.setLayout(sl_panel);

		JLabel lblNewLabel = new JLabel("");
		sl_panel.putConstraint(SpringLayout.NORTH, lblNewLabel, 10,
				SpringLayout.NORTH, panel);
		sl_panel.putConstraint(SpringLayout.WEST, lblNewLabel, 85,
				SpringLayout.WEST, panel);
		sl_panel.putConstraint(SpringLayout.SOUTH, lblNewLabel, -10,
				SpringLayout.SOUTH, panel);
		sl_panel.putConstraint(SpringLayout.EAST, lblNewLabel, -226,
				SpringLayout.EAST, panel);
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel
				.setIcon(new ImageIcon(
						"/Users/maximebobin/DevProjects/medicfile-mbo-asb/img/white-icon/default-icon-small.png"));
		panel.add(lblNewLabel);

		JLabel lblPreferences = new JLabel("Welcome");
		sl_panel.putConstraint(SpringLayout.SOUTH, lblPreferences, -11,
				SpringLayout.SOUTH, lblNewLabel);
		lblPreferences.setForeground(new Color(128, 128, 128));
		lblPreferences.setFont(new Font("Helvetica Neue", Font.PLAIN, 20));
		panel.add(lblPreferences);

		JLabel lblNewLabel_1 = new JLabel("Medic Files");
		sl_panel.putConstraint(SpringLayout.NORTH, lblPreferences, 6,
				SpringLayout.SOUTH, lblNewLabel_1);
		sl_panel.putConstraint(SpringLayout.WEST, lblPreferences, 10,
				SpringLayout.WEST, lblNewLabel_1);
		sl_panel.putConstraint(SpringLayout.NORTH, lblNewLabel_1, 10,
				SpringLayout.NORTH, panel);
		sl_panel.putConstraint(SpringLayout.SOUTH, lblNewLabel_1, -44,
				SpringLayout.SOUTH, panel);
		sl_panel.putConstraint(SpringLayout.WEST, lblNewLabel_1, 6,
				SpringLayout.EAST, lblNewLabel);
		lblNewLabel_1.setFont(new Font("Open Sans", Font.PLAIN, 32));
		lblNewLabel_1.setForeground(new Color(0, 0, 0));
		panel.add(lblNewLabel_1);

		passwordTextField = new JPasswordField();
		sl_contentPanel.putConstraint(SpringLayout.NORTH, passwordTextField, 6,
				SpringLayout.SOUTH, lblPleaseCreateA);
		sl_contentPanel.putConstraint(SpringLayout.WEST, passwordTextField, 0,
				SpringLayout.WEST, lblPleaseCreateA);
		sl_contentPanel.putConstraint(SpringLayout.EAST, passwordTextField,
				-232, SpringLayout.EAST, contentPanel);
		contentPanel.add(passwordTextField);

		final JLabel errorLabel = new JLabel("");
		sl_contentPanel.putConstraint(SpringLayout.NORTH, errorLabel, 11,
				SpringLayout.SOUTH, folderTextField);
		sl_contentPanel.putConstraint(SpringLayout.WEST, errorLabel, 34, SpringLayout.WEST, contentPanel);
		sl_contentPanel.putConstraint(SpringLayout.SOUTH, errorLabel, 0,
				SpringLayout.SOUTH, contentPanel);
		sl_contentPanel.putConstraint(SpringLayout.EAST, errorLabel, -25, SpringLayout.EAST, contentPanel);
		errorLabel.setFont(new Font("Open Sans", Font.PLAIN, 11));
		errorLabel.setForeground(new Color(255, 69, 0));
		errorLabel.setVerticalAlignment(SwingConstants.TOP);
		errorLabel.setHorizontalAlignment(SwingConstants.LEFT);
		contentPanel.add(errorLabel);
		{
			JPanel buttonPane = new JPanel();
			FlowLayout fl_buttonPane = new FlowLayout(FlowLayout.RIGHT);
			fl_buttonPane.setVgap(10);
			buttonPane.setLayout(fl_buttonPane);
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				// ============================================================
				// OK BUTTON
				// ============================================================
				JButton okButton = new JButton("OK");
				okButton.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {

						if (folderTextField.getText().toString().trim()
								.compareTo("") != 0
								&& passwordTextField.getText().toString()
										.trim().compareTo("") != 0) {
							
							PrefProperties.workingDirectory = folderTextField
									.getText();
							PrefProperties.password = passwordTextField
									.getText();
							PrefProperties.isFirtTime = false;
							
							// System.out.println(PrefProperties.isFirtTime);
							// System.out.println(PrefProperties.password);
							// System.out.println(PrefProperties.workingDirectory.toString());

							PrefProperties.savePreferences();
							MyTrayIcon.enableMenu();
							PrefProperties.launchRoutine();
							PrefProperties1.launchRoutine();
							PrefProperties2.launchRoutine();
							PrefProperties3.launchRoutine();
							
							dispose();
						} else {

							StringBuilder sb = new StringBuilder(64);
							sb.append("<html>Please crerate a password and choose a directory before proceeding.</html>");

							errorLabel.setText(sb.toString());
						}
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setEnabled(false);
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

	/* Triggers the file picker */
	public void triggerFilePicker() {
		
		// If it's a mac we can display the native file picker
		if (PrefProperties.isMac()) {
			System.setProperty("apple.awt.fileDialogForDirectories", "true");
			FileDialog fd = new FileDialog(GUIWelcomeDialog.this);
			fd.setDirectory(System.getProperty("user.home"));
			fd.setLocation(50, 50);
			fd.setVisible(true);
			if (fd.getFile() != null) {
				File selectedFile = new File(fd.getFile());
				File selectedDirectory = new File(fd.getDirectory());
				System.setProperty("apple.awt.fileDialogForDirectories",
						"false");

				System.out.println(selectedDirectory + "/"
						+ selectedFile.getAbsolutePath());

				folderTextField.setText(selectedDirectory + "/" + selectedFile);
			}

		}
		// No Mac, display the odd Java file picker
		else {
			
			JFileChooser fc = new JFileChooser();

			// only directories can be chosen
			fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

			// add the dialog to the desktop
			int returnVal = fc.showOpenDialog(GUIWelcomeDialog.this);

			if (returnVal == JFileChooser.APPROVE_OPTION) {
				File file = fc.getSelectedFile();
				
				// This is where a real application would open the file.
				folderTextField.setText(file.getAbsolutePath());
			}

		}
	}
}
